/*
 * @Author: 噜噜噜 1163376226@qq.com
 * @Date: 2025-04-17 16:56:02
 * @LastEditors: 噜噜噜 1163376226@qq.com
 * @LastEditTime: 2025-04-25 16:15:42
 * @FilePath: /auto/automation-project/js/indexModule.js
 * @Description: 首页功能模块
 */

import apiConfig from "../js/apiConfig.js";

export function init() {
  // 绑定导航按钮点击事件
  document
    .querySelector(".nav-button[href='add-module.html']")
    .addEventListener("click", (e) => {
      e.preventDefault();
      window.location.href = "add-module.html";
    });

  document
    .querySelector(".nav-button[href='add-element.html']")
    .addEventListener("click", (e) => {
      e.preventDefault();
      window.location.href = "add-element.html";
    });

  document
    .querySelector(".nav-button[href='scenario.html']")
    .addEventListener("click", (e) => {
      e.preventDefault();
      window.location.href = "scenario.html";
    });
  // 绑定运行测试按钮点击事件
  document.getElementById("runTest")?.addEventListener("click", () => {
    const scenarioId = document.getElementById("testScenario").value;

    if (!scenarioId) {
      alert("请先选择测试场景");
      return;
    }

    // 调用测试接口
    fetch(apiConfig.getURL("tests", "status") + "&id=" + scenarioId)
      .then((response) => response.json())
      .then((data) => {
        if (data.code === 200) {
          console.log("测试结果:", data);
          alert(data.msg || "测试已开始，请等待测试响应");
        } else {
          throw new Error(data.msg || "测试启动失败");
        }
      })
      .catch((error) => {
        console.error("测试失败:", error);
        alert("测试失败: " + error.message);
      });
  });

  loadScenarios();
}

// 获取测试场景数据
function loadScenarios() {
  const scenarioSelect = document.getElementById("testScenario");

  fetch(apiConfig.getURL("scenarios", "list"))
    .then((response) => response.json())
    .then((data) => {
      // 清空现有选项
      scenarioSelect.innerHTML = "";

      // 检查响应码
      if (data.code === 200 && data.msg && data.msg.length > 0) {
        // 添加默认选项
        const defaultOption = document.createElement("option");
        defaultOption.value = "";
        defaultOption.textContent = "-- 请选择测试场景 --";
        scenarioSelect.appendChild(defaultOption);

        // 添加场景选项
        data.msg.forEach((scenario) => {
          const option = document.createElement("option");
          option.value = scenario.scenariosId || "";
          option.textContent = scenario.scenariosName || "未命名场景";
          scenarioSelect.appendChild(option);
        });
      } else if (data.code === 404) {
        // 404状态的处理 - 直接显示"没有找到相关的场景"并禁用下拉
        const emptyOption = document.createElement("option");
        emptyOption.value = "";
        emptyOption.textContent = "没有找到相关的场景";
        scenarioSelect.appendChild(emptyOption);
        scenarioSelect.disabled = true;
      } else {
        // 其他错误状态
        const errorOption = document.createElement("option");
        errorOption.value = "";
        errorOption.textContent = data.msg || "获取场景失败";
        scenarioSelect.appendChild(errorOption);
        scenarioSelect.disabled = true;
      }
    })
    .catch((error) => {
      console.error("获取测试场景失败:", error);
      scenarioSelect.innerHTML = '<option value="">加载场景失败</option>';
      scenarioSelect.disabled = true;
    });
}
